package com.athu.study.controller;

import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Base64;

@RestController
@Slf4j
public class readImageController {
    @Autowired
    private ChatModel chatModelQwen;
    @Value("classpath:static/images/ji.jpg")
    private Resource resource;
    @GetMapping(value = "/image/call")
    public String readImageContent() throws IOException {
        byte[] byteArray = resource.getContentAsByteArray();
        String string = Base64.getEncoder().encodeToString(byteArray);
        UserMessage userMessage = UserMessage.from(TextContent.from("从这张图片谈及娱乐新闻，你能读出什么"),
                ImageContent.from(string, "image/jpg"));
        ChatResponse chatResponse = chatModelQwen.chat(userMessage);
        String result = chatResponse.aiMessage().text();
        return result;
    }
}
